package com.admission.util;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.admission.model.ArrayInfo;
import com.admission.service.ArrayInfoManager;
import com.admission.service.EnvironmentManager;

@Repository
@Transactional
public class ArrayUtils {
	private ArrayInfoManager arrayInfoManager;
	private EnvironmentManager environmentManager;
	public Map<String, Map<String, String>> getSchoolLifeMap() {
		TreeMap<String, Map<String, String>> map = new TreeMap<String, Map<String, String>>();
		List<ArrayInfo> l = arrayInfoManager.getSchoolLife();
		for (ArrayInfo arrayInfo : l) {
			TreeMap<String, String> pmap = new TreeMap<String, String>();
			List<ArrayInfo> ail = arrayInfo.getArrayInfos();
			for (ArrayInfo arrayInfo2 : ail) {
				String a =arrayInfo2.getItemName();
				if(environmentManager.getByValue((a)))
						{
				pmap.put(arrayInfo2.getItemName(), arrayInfo2.getItemValue());
				}
			}
			map.put(arrayInfo.getItemName(), pmap);
		}
		
//		Map map2 = (Map) map.values();
//		List list =(List) map.values();
//		this.environmentManager.getPageByValue(value)
		return map;
	}

	public String getCollegeSwitch() {
		return arrayInfoManager.getByItName(ArrayInfo.COLLEGECONSULT)
				.getItemValue();
	}

	public String getArtSwitch() {
		return arrayInfoManager.getByItName(ArrayInfo.ARTCONSULT)
				.getItemValue();
	}

	/**
	 * 获取所有的key value 列表
	 * 
	 * @return
	 */
	public Map<String, String> getMap() {
		TreeMap<String, String> map = new TreeMap<String, String>();
		List<ArrayInfo> l = arrayInfoManager.getAll();
		for (ArrayInfo arrayInfo : l) {
			map.put(arrayInfo.getItemName(), arrayInfo.getItemValue());
		}
		return map;
	}

	/**
	 * 获取省列表
	 * 
	 * @return
	 */
	public Map<String, String> getProvinceMap() {
		TreeMap<String, String> map = new TreeMap<String, String>();
		List<ArrayInfo> l = arrayInfoManager.getProvinces();
		for (ArrayInfo arrayInfo : l) {
			String t = String.valueOf(arrayInfo.getOrderNo());
			if (t.length() < 2) {
				t = "0" + t;
			}
			map.put(t, arrayInfo.getItemValue());
		}
		return map;
	}

	/**
	 * 获取科类
	 * 
	 * @return
	 */
	public Map<String, String> getType() {
		TreeMap<String, String> map = new TreeMap<String, String>();
		List<ArrayInfo> l = arrayInfoManager.getTypes();
		for (ArrayInfo arrayInfo : l) {
			String t = String.valueOf(arrayInfo.getOrderNo());
			if (t.length() < 2) {

				t = "0" + t;
			}
			map.put(t, arrayInfo.getItemValue());
		}
		return map;

	}

	/**
	 * 通过value拿到key,支持模糊匹配
	 * 
	 * @param map
	 * @param value
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public Object getKey(Map map, Object value) {
		Object o = null;
		for (Object key : map.keySet()) {
			if (map.get(key).equals(value))
				return key;
			if (((String) map.get(key)).indexOf(((String) value)) >= 0) {
				o = key;
			}
		}
		return o;
	}

	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}

	public EnvironmentManager getEnvironmentManager() {
		return environmentManager;
	}
	@Autowired
	public void setEnvironmentManager(EnvironmentManager environmentManager) {
		this.environmentManager = environmentManager;
	}

	public ArrayInfoManager getArrayInfoManager() {
		return arrayInfoManager;
	}

}
